<?php
namespace app\mouth\model;
use think\Db;
use app\mouth\common\Encapsulation;
class CircleUserModel extends Encapsulation{
    public function fans($table,$where,$flied){
       return $this->all($table,$where,$flied);
    }
    public function Myarticle($id,$uid){
       $date['content'] = $this->one('circle_invitation',['id'=>$id,'u_id'=>$uid],'*');
       $date['comment'] = $this->all('circle_comment',['i_id'=>$id],'*');
       return $date;
    }
    public static function personalinformation($id,$uid){
       $date['information']     = Db::name('circle_user')->field('portrait,gender,nickname')->where('id',$id)->find();//个人信息
       $date['follow']          = Db::name('circle_follow')->where('main_id',$id)->count();//关注人数
       $date['fans']            = Db::name('circle_fans')->where('main_id',$id)->count();//粉丝人数
       $date['content']         = Db::name('circle_invitation')->where('u_id',$id)->select();//帖子
       $where['id']             = Db::name('circle_invitation')->where('u_id',$id)->column('c_id');//加入的圈子id
       $date['circle']          = Db::name('circle')->where($where)->select();//加入的圈子id
       $mutual                  = Db::name('circle_follow')->where('main_id',$id)->where('u_id',$uid)->find();//是否关注
       $date['mutual'] = intval($mutual);
       return $date;
    }
    public static function It($id){
        $i_id = Db::name('circle_like')->where('u_id',$id)->column('i_id');
        if (empty($i_id)){
            return [];
        }
        $i_id = implode(',',$i_id);
        $value = Db::name('circle_invitation')->where('id',$i_id)->select();
        return $value;
    }
    public static function allPosts($id){
        $value = Db::name('circle_invitation')->where('u_id',$id)->select();
        return $value;
    }
    public function establishCircle($value,$id){

        $index = $this->add($value);
        if (!is_array($index)){
            return $index;
        }
        $value['u_id'] = $id;
        $value['time'] = time();
        unset($value['openid']);
        Db::name('circle')->insert($value);
        return true;
    }

    public  function add($date){
        if (empty($date['level'])){
            return '请选择分类';
        }
        if (empty($date['describe'])){
            return '请添加描述';
        }
        if (empty($date['name'])){
            return '请添加姓名';
        }
        if (empty($date['cover'])){
            return '请添图片';
        }
        if (empty($date['background'])){
            return '请添图片';
        }
     return $date;
    }
}